package com.example.ui.dialog;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;

import com.example.ui.R;


/**
 * 加载中提示对话框<br/>
 * 支持自定义布局<br/>
 * 支持样式修改，如颜色，背景等<br/>
 * 更多自定义设置详见接口{@link LoadingDialogFragment.Builder}<br/>
 * 用法示例：<br/>
 * <pre>
 *  LoadingDialogFragment.newBuilder()
 *                 .setHint("加载中",-1,-1)
 *                 .setCancelable(false)
 *                 .create()
 *                 .show(getSupportFragmentManager(), "");
 * </pre>
 */
public class LoadingDialogFragment extends AppCompatDialogFragment {
    private Builder mBuilder;

    private LoadingDialogFragment(Builder builder) {
        mBuilder = builder;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //设置对话框内部背景透明
        }
        if (mBuilder.layoutResId != -1) {
            return inflater.inflate(mBuilder.layoutResId, container, false);
        }
        return inflater.inflate(R.layout.junmeng_dialog_loading_hint, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        View dialogView = view.findViewById(R.id.dialog);
        if (mBuilder.dialogBackground != -1) {
            dialogView.setBackgroundResource(mBuilder.dialogBackground);
        }

        TextView stateView = view.findViewById(R.id.tvStateText);
        if (TextUtils.isEmpty(mBuilder.hintText)) {
            stateView.setVisibility(View.GONE);
        } else {
            stateView.setVisibility(View.VISIBLE);
            stateView.setText(mBuilder.hintText);
            if (mBuilder.hintColor != -1) {
                stateView.setTextColor(mBuilder.hintColor);
            }
            if (mBuilder.hintSize != -1) {
                stateView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mBuilder.hintSize);
            }
        }

        ProgressBar loadingView = view.findViewById(R.id.loading);
        if (mBuilder.loadingColor != -1) {
            loadingView.setIndeterminateTintList(ColorStateList.valueOf(mBuilder.loadingColor));
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        if (mBuilder.backgroundTransparent) {
            //背景透明
            if (getDialog() == null || getDialog().getWindow() == null) {
                return;
            }
            Window window = getDialog().getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不加这一行状态栏可能变白
            WindowManager.LayoutParams windowParams = window.getAttributes();
            windowParams.dimAmount = 0.0f;
            window.setAttributes(windowParams);
        }
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder {
        private String hintText = null;
        private int hintColor = -1;
        private float hintSize = -1;
        private int loadingColor = -1;
        private int layoutResId = -1;
        private boolean backgroundTransparent = false;
        private int dialogBackground = -1;
        private boolean isCancelable = true;

        /**
         * 设置加载提示文字及样式
         *
         * @param hint   为空表示不显示提示文字
         * @param color  文字颜色,-1表示默认
         * @param sizeSP 文字大小，单位sp,-1表示默认
         * @return
         */
        public Builder setHint(String hint, @ColorInt int color, float sizeSP) {
            hintText = hint;
            hintColor = color;
            hintSize = sizeSP;
            return this;
        }

        /**
         * 设置对话框背景
         *
         * @param drawableResId -1表示默认
         * @return
         */
        public Builder dialogBackground(@DrawableRes int drawableResId) {
            dialogBackground = drawableResId;
            return this;
        }

        /**
         * 设置转圈颜色
         *
         * @param color -1表示默认
         * @return
         */
        public Builder loadingColor(@ColorInt int color) {
            loadingColor = color;
            return this;
        }

        /**
         * 背景是否透明，非对话框背景
         *
         * @param isTransparent 默认false
         * @return
         */
        public Builder backgroundTransparent(boolean isTransparent) {
            this.backgroundTransparent = isTransparent;
            return this;
        }

        /**
         * 设置自定义布局
         * 自定义布局中的id必须与R.layout.dialog_hint保持一致
         *
         * @param layoutResId
         * @return
         */
        public Builder setCustomLayout(@LayoutRes int layoutResId) {
            this.layoutResId = layoutResId;
            return this;
        }

        /**
         * 设置点击是否消失
         *
         * @param isCancelable 默认true
         * @return
         */
        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        public LoadingDialogFragment create() {
            LoadingDialogFragment dialogFragment = new LoadingDialogFragment(this);
            dialogFragment.setCancelable(isCancelable);
            return dialogFragment;
        }
    }
}
